//
//  MKMarinTabBarController.swift
//  101weibo
//
//  Created by 舒磊明 on 2019/1/17.
//  Copyright © 2019年 劢克科技. All rights reserved.
//

import UIKit

class MKMarinTabBarController: UITabBarController {

    //Tabbar Center Btn
    private lazy var composeBtn = UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setChildController()
        setCenterBtn()
    }
}

//MARK: - 设置当前控制器UI
extension MKMarinTabBarController {
    private func setCenterBtn() {
        composeBtn.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.height / 2)
        composeBtn.addTarget(self, action: #selector(composeClick), for: .touchUpInside)
        tabBar.addSubview(composeBtn)
    }
}

//MARK: - 控制器事件
extension MKMarinTabBarController {
    @objc private func composeClick() {
        print("Tabbar center btn clicked!")
    }
}

//MARK: - 添加TabbarController子控制器
extension MKMarinTabBarController {
    private func setChildController() {
        addChild(MKHomeTableViewController(), title: "首页", image: "tabbar_home")
        addChild(MKMessageTableViewController(), title: "消息", image: "tabbar_message_center")
        
        let nullVc = MKNullTableViewController()
        nullVc.tabBarItem.isEnabled = false
        addChild(nullVc)
        
        addChild(MKDiscoverTableViewController(), title: "发现", image: "tabbar_discover")
        addChild(MKProfileTableViewController(), title: "我的", image: "tabbar_profile")
    }
    
    private func addChild(_ childVc: UIViewController, title: String, image: String) {
        childVc.title = title
        childVc.tabBarItem.image = UIImage(named: image)
        childVc.tabBarItem.selectedImage = UIImage(named: image + "_highlighted")
        let childNav = UINavigationController(rootViewController: childVc)
        
        addChild(childNav)
    }
}
